import {
	resPost
} from '@/common/http.request.js';
import Store from '@/store/index.js'
export const configMixin = {
	data() {
		return {
			dataList: null, 
			parameter: null,
			apiurl: '',
			apiParams: {}, 
			queryObj: {}, 
			pageNo: 1,
			pageCount: 1,
			loading: false,
			relationParams: {}, 
			isComponent: false, 
		}
	},
	watch: {
		templateItem: {
			handler(item) {
				if (item.code) {
					this.init(item);
				}
			},
			immediate: true
		}
	},
	props: ["templateItem"],
	methods: {
		// 初始化
		init(item) {
			return new Promise((resolve, reject) => {
				this.parameter = item.parameter
				this.apiurl = item.apiurl
				let apiParams = this.analysisApiParams(item.apiParams || [])
				this.apiParams = apiParams
				if (item.pageData && item.pageData[0]) {
					this.dataList = item.pageData
					resolve()
				} else if (item.apiurl) {
					this.dataList = null
					this.pageNo = 1
					this.getDataList({}).then(() => {
						resolve()
					})
				} else {
					this.dataList = []
					resolve()
				}
			})
		},
		// 解析apiParams
		analysisApiParams(json) {
			let obj = {}
			let queryObj = {}
			json.forEach((paramItem) => {
				if (paramItem.inputType == 'category' && paramItem.name == 'forEach') {
					// 多级分类
					let arr = Object.keys(paramItem.value || {})
					arr.forEach((key) => {
						if (key != 'value') {
							obj[key] = paramItem.value[key]
						}
					})
				}else if(paramItem.inputType == 'webGet'){
					if(paramItem.value){
						queryObj[paramItem.name] = paramItem.url
					}
				} else if (paramItem.value !== null && paramItem.value !== undefined && paramItem.value !==
					'') {
					obj[paramItem.name] = paramItem.value
				}
			})
			this.queryObj = {
				...queryObj
			}
			return obj
		},
		/*
		 * 加载列表
		 * obj:{} // 新补进来的查询参数
		 * bol Boolean 是否关联查询重置列表数据
		 * */
		getDataList(obj = {}, bol) {
			if (this.loading) {
				return
			}
			this.loading = true
			return new Promise((resolve, reject) => {
				if (!this.templateItem.pageData[0] && this.templateItem.apiurl) {
					let oldRelation = this.relationParams
					oldRelation = {
						...oldRelation,
						...obj || {},
					}
					let arr = Object.keys(oldRelation)
					let relationParams = {}
					arr.forEach((key) => {
						if (oldRelation[key] !== null && oldRelation[key] !== undefined) {
							relationParams[key] = oldRelation[key]
						}
					})
					let query = this.getQueryObj()
					let requestParams = {
						page: this.pageNo,
						...this.apiParams,
						...query,
						...relationParams
					}
					resPost(this.apiurl, requestParams, {}).then((res) => {
						this.relationParams = relationParams
						this.pageCount = res.pageCount
						if (this.parameter.isWaterfall) {
							if (this.pageNo == 1) {
								this.dataList = null
							}
							this.$nextTick(() => {
								this.dataList = res.data || []
							})
						} else {
							let dataList = bol ? [] : (this.dataList || [])
							this.dataList = dataList.concat(res.data || [])
						}
						setTimeout(() => {
							this.loading = false
						}, 500)
						resolve()
					}).catch(() => {
						this.loading = false
						reject()
					})
				} else {
					this.loading = false
					resolve()
				}
			})
		},
		/*
		 * 关联查询、重置刷新
		 * obj {} // 查询参数
		 **/
		resetSearch(obj) {
			this.pageNo = 1
			this.pageCount = 1
			this.isComponent = true 
			this.getDataList(obj || {}, true)
		},
		// 加载更多
		getMoreList() {
			let parameter = this.parameter
			if (this.templateItem.pageData[0]) {
				return
			}
			if (this.apiurl && parameter.moreAble && this.pageNo < this.pageCount) { 
				this.pageNo = this.pageNo + 1
				uni.showLoading({
					mask: true,
					title: '加载中...'
				})
				this.getDataList({}).then(() => {
					if (!this.parameter.isWaterfall) {
						uni.hideLoading()
					}
				}).catch(() => {
					uni.hideLoading()
				})
			}
		},
		// 从项目实时取值
		getQueryObj () {
			let obj = {}
			let arr = Object.keys(this.queryObj)
			if(arr[0]){
				arr.forEach((key)=>{
					let keyArr = key.split(';')
					if(keyArr[1]){
						let value = Store.state[this.queryObj[key]][keyArr[1]]
						if(value || value === 0){
							obj[keyArr[0]] = value
						}
					}else if(key === 'forEach'){
						let storeObj = Store.state[this.queryObj[key]] || {}
						let arr2 = Object.keys(storeObj)
						arr2.forEach((key2)=>{
							let val2 = storeObj[key2]
							if(val2 || val2 === 0){
								obj[key2] = val2
							}
						})
					}else{
						let value = Store.state[this.queryObj[key]]
						if(value || value === 0){
							obj[key] = value
						}
					}
				})
			}
			return obj
		}
	}

}
